#include "common.h"

int main()
{
    //获取server创建的共享内存,也就是key值
    key_t key = ftok(PATH_NAME, PROJ_ID);
    if (key < 0)
    {
        perror("ftok");
        return 1;
    }

    cout << "key : " << key << endl;

    //client这里只需要获取共享内存的id即可
    int shmid = shmget(key, SIZE, IPC_CREAT);
    if (shmid < 0)
    {
        perror("shmget");
        return 2;
    }

    //共享内存的挂接
    char* mem = (char*)shmat(shmid, NULL, 0);
    cout << "client process attaches success!" << endl;
    sleep(5);
    
    //我们实现共享内存通信的地方
    char c = 'A';
    while(c <= 'Z')
    {
        mem[c - 'A'] = c;
        c++;
        mem[c - 'A'] = '\0';
        sleep(1);
    }

    //共享内存的去挂接
    shmdt(mem);
    cout << "client process detaches success!" << endl;
    
    //client端不需要共享内存的释放,释放由server端进行

    return 0;
}
